<?php

// 本文档自动生成，仅供测试运行
class IndexAction extends Action {

    /**
      +----------------------------------------------------------
     * 默认操作
      +----------------------------------------------------------
     */
    public function index() {
        $db = D('ImgView');
        $arr = $db->limit(10)->order('id desc')->select();
        foreach ($arr as $k => $v) {
            foreach ($v as $val) {
                $db = M('good');
                if ($num = $db->where(array('mid' => $v['id']))->count()) {
                    $arr[$k]['num'] = $num;
                } else {
                    $arr[$k]['num'] = '';
                }
            }
        }
        $_SESSION['start'] = 0;
        $this->assign('img', $arr);
        $this->display();
    }

    //
    public function ajax() {
        $num = $_POST['num'] * 10;
        $db = D('ImgView');
        if (!$_POST['kid']) {
            $arr = $db->limit("$num ,10")->order('id desc')->select();
        } else {
            $arr = $db->where(array('kid' => $_POST['kid']))->limit("$num,10")->order('id desc')->select();
        }
        foreach ($arr as $k => $v) {
            $db = M('good');
            if ($num = $db->where(array('mid' => $v['id']))->count()) {
                $arr[$k]['num'] = $num;
            } else {
                $arr[$k]['num'] = '';
            }
            $db = D('CommentView');
            $sort = $db->where("mid=" .$v['id'])->order("id asc")->select();
            if($sort){
                $arr[$k]['sort'] = $sort;
            }
        }
        if ($arr) {
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //显示登录
    public function enter() {
        $this->display();
    }

    //显示QQ注册
    public function QQ() {
        $this->display();
    }

    //用qq登录
    public function QQlogin() {
        $this->display();
    }

    //qq登录异步验证
    public function qqver() {
        $qqname = htmlspecialchars($_POST['qqname']);
        $qqpassword = md5(htmlspecialchars($_POST['qqpassword']));
        $db = M('user');
        $arr = $db->where(array('qqname' => $qqname, 'qqpassword' => $qqpassword))->find();
        if ($arr) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //验证用户名
    public function qqenter() {
        $qqname = htmlspecialchars($_POST['username']);
        $db = M('user');
        $arr = $db->where(array('qqname' => $qqname))->find();
        if (!$arr) {
            $data = array(
                "qqname" => $qqname,
                'qqpassword' => md5($_POST["password"])
            );
            $uid = $db->add($data);
            $db = M('userinfo');
            $db->add(array('pid' => $uid));
            $_SESSION['qqname'] = $qqname;
            $_SESSION['uid'] = $uid;
            $this->redirect('relayLogin');
        } else {
            $this->redirect('relayEnter', $arr);
        }
    }

    //qq注册成功显示
    public function relayLogin() {
        $this->display();
    }

    //用户完善个人资料异步验证
    public function relayver() {
        $uname = htmlspecialchars($_POST['uname']);
        $username = htmlspecialchars($_POST['username']);
        $db = M('user');
        $isuname = $db->where(array('uname' => $uname))->find();
        if ($isuname) {
            echo json_encode(array('num' => 1));
            die;
        }
        $isuser = $db->where(array('username' => $username))->find();
        if ($isuser) {
            echo json_encode(array('num' => 2));
            die;
        }
        echo json_encode(array('num' => 3));
    }

    //写入完善的个人资料
    public function relayuser() {
        $data = $_POST;
        $db = M('user');
        if ($db->where(array('uid' => $_SESSION['uid']))->save($data)) {
            $thump = $db->where(array('uid' => $_SESSION['uid']))->find();
            $_SESSION['thump'] = $thump['thump'];
            $_SESSION['uname'] = $_POST['uname'];
            //完善好后跳转到第一次登录的第一个页面
            $this->redirect('Relay/first');
        }
    }

    //但注册的QQ已经存在的时候
    public function relayEnter() {
        $this->display();
    }

    //登录
    public function tologin() {
        $this->display();
    }

    //异步验证帐号是否正确
    public function verification() {
        $username = htmlspecialchars($_POST['username']);
        $password = htmlspecialchars($_POST['password']);
        $db = M('user');
        $arr = $db->where(array('username' => $username, 'password' => $password))->find();
        if ($arr) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //登录
    public function gologin() {
        $username = htmlspecialchars($_POST['username']);
        $password = htmlspecialchars($_POST['password']);
        $db = M('user');
        $arr = $db->where(array('username' => $username, 'password' => $password))->find();
        if ($arr) {
            $_SESSION['uid'] = $arr['uid'];
            $_SESSION['thump'] = $arr['thump'];
            $_SESSION['uname'] = $arr['uname'];
            $this->redirect('Complete/index');
        }
    }

    //退出登录
    public function quit() {
        $_SESSION = array();
        session_destroy();
        session_unset();
        $this->redirect('index');
    }

    private static $files;

    public function upload() {
        import('ORG.Net.UploadFile');
        $file = './static/' . date("ymdH", time());
        is_dir($file) || mkdir($file, 0777, true);
        if (self::$files) {
            unlink(self::$files);
        }
        $upload = new UploadFile();
        $upload->saveRule = time();
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath = $file . "/";
        $upload->upload();
        $arr = $upload->getUploadFileInfo();
        $file = "/huaban/index" . substr($arr[0]['savepath'], 1) . $arr[0]['savename'];
        self::$files = $file;
        echo self::$files;
    }

    //显示画板
    public function huaban() {
        $db = D('SkedchpatView');
        $arr = $db->limit(10)->order('kid desc')->select();
        foreach ($arr as $k => $v) {
            if (!isset($_SESSION['uid'])) {
                $arr[$k]['atten'] = 0;
            } else {
                $db = M('atten');
                $uid = $_SESSION['uid'];
                if ($db->where(array('uid' => $uid, 'kid' => $v['kid']))->find()) {
                    $arr[$k]['atten'] = 1;
                } else {
                    $arr[$k]['atten'] = 0;
                }
            }
        }
        $this->assign('hua', $arr);
        $this->display();
    }

    //异步读取画板
    public function ajaxhua() {
        $num = $_POST['num'] * 10;
        $db = D('SkedchpatView');
        $arr = $db->limit("$num ,10")->order('kid desc')->select();
        if ($arr) {
            foreach ($arr as $k => $v) {
                if (!isset($_SESSON['uid'])) {
                    $arr[$k]['atten'] = 0;
                }
                $db = M('img');
                $img = $db->where('sid=' . $v['kid'])->limit(9)->select();
                $arr[$k]['img'] = $img;
                if ($db->table('hua_atten')->where(array('uid' => $_SESSION['uid'], 'kid' => $v['kid']))->find()) {
                    $arr[$k]['atten'] = 1;
                } else {
                    $arr[$k]['atten'] = 0;
                }
            }
            echo json_encode($arr);
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //添加评论
    public function addcom() {
        $data = array(
            'uid' => $_SESSION['uid'],
            'mid' => (int) $_POST['id'],
            'content' => htmlspecialchars($_POST['cont']),
            'time' => time()
        );
        $db = M('comment');
        if ($id = $db->add($data)) {
            echo json_encode(array('num' => 1, 'uname' => $_SESSION['uname'], 'id' => $id));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //删除评论
    public function delcom() {
        $id = (int) $_POST['id'];
        $db = M('comment');
        if ($db->where(array('id' => $id))->delete()) {
            echo json_encode(array('num' => 1));
        } else {
            echo json_encode(array('num' => 0));
        }
    }

    //称赞
    public function good() {
        $id = (int) $_POST['id'];
        $data = array(
            'mid' => $id,
            'uid' => $_SESSION['uid']
        );
        $db = M('good');
        $arr = $db->where($data)->find();
        if ($arr) {
            if ($db->where($data)->delete()) {
                echo json_encode(array('num' => 1));
                die;
            }
        } else {
            if ($db->add($data)) {
                echo json_encode(array('num' => 2));
            }
        }
    }

}

?>